package com.dkd.day20250616;

import java.util.Stack;

public class SolutionisValid {
    public static void main(String[] args) {

    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param s string字符串
     * @return bool布尔型
     */
        public boolean isValid (String s) {
            // write code here
            //建立辅助栈
            if(s.length()==1) return false;
            Stack<Character> stack = new Stack<Character>();
            for (int i = 0; i <= s.length()-1; i++) {
                if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
                    stack.push(s.charAt(i));
                } else if (s.charAt(i) == ')') {
                    if (stack.isEmpty() || stack.peek() != '(') {
                        return false;
                    } else stack.pop();
                } else if (s.charAt(i) == '}') {
                    if (stack.isEmpty() ||stack.peek() != '{') {
                        return false;
                    } else stack.pop();
                } else if (s.charAt(i) == ']') {
                    if (stack.isEmpty() ||stack.peek() != '[') {
                        return false;
                    } else stack.pop();
                }
            }
            return stack.isEmpty();
        }

}
